<template>
  <Transition name="modal">
    <div class="modal" v-if="visible" @click.self="closeModal">
      <div class="modal-content">
        <div class="modal-header">
          <button class="close-btn" @click="closeModal" v-if="!showSuccess">×</button>
        </div>
        <div class="modal-body">
          <Transition name="fade">
            <div v-if="showSuccess" class="success-message">
              <div class="success-icon">
                <svg viewBox="0 0 24 24" class="checkmark">
                  <path 
                    fill="none" 
                    stroke="currentColor" 
                    stroke-width="2" 
                    d="M4 12l5 5L20 7"
                  />
                </svg>
              </div>
              <p>登录成功</p>
            </div>
          </Transition>
          
          <slot v-if="!showSuccess"></slot>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue';

const visible = ref(false);
const showSuccess = ref(false);

const emit = defineEmits(['reset-forms', 'auth-success']);

const handleAuthSuccess = () => {
  showSuccess.value = true;
  setTimeout(() => {
    showSuccess.value = false;
    closeModal();
    emit('auth-success');
  }, 1500);
};

const closeModal = () => {
  visible.value = false;
  showSuccess.value = false;
  emit('reset-forms');
};

const openModal = () => {
  visible.value = true;
  showSuccess.value = false;
  emit('reset-forms');
};

defineExpose({ openModal, closeModal, handleAuthSuccess });
</script>

<style scoped lang="scss">
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;

  .modal-content {
    position: relative;
    background: var(--bgc-color-1);
    border-radius: 16px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.3);
    min-width: 320px;
    max-width: 90%;
    margin: 20px;
    overflow: hidden;
    transition: all 0.3s ease;

    // 成功状态样式
    &.success {
      background: var(--bgc-color-2);
      transform: scale(0.95);
    }
  }

  .modal-header {
    position: absolute;
    top: 0;
    right: 0;
    padding: 16px;
    z-index: 1;

    .close-btn {
      background: none;
      border: none;
      color: var(--secondary-color);
      font-size: 24px;
      cursor: pointer;
      padding: 4px 8px;
      border-radius: 50%;
      transition: all 0.3s;

      &:hover {
        color: var(--primary-color);
        background: color-mix(in srgb, var(--hover-color) 10%, transparent);
      }
    }
  }

  .modal-body {
    padding: 24px;
    position: relative;
  }

  // 成功提示样式
  .success-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    color: var(--primary-color);

    .success-icon {
      width: 60px;
      height: 60px;
      margin-bottom: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: color-mix(in srgb, var(--hover-color) 10%, transparent);
      border-radius: 50%;
      animation: scaleIn 0.3s ease;

      .checkmark {
        width: 32px;
        height: 32px;
        color: var(--hover-color);
        animation: drawCheck 0.5s ease forwards;
      }
    }

    p {
      font-size: 18px;
      font-weight: 500;
      animation: fadeInUp 0.3s ease 0.2s both;
    }
  }
}

// 动画
@keyframes scaleIn {
  from {
    transform: scale(0);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes drawCheck {
  from {
    stroke-dashoffset: 100;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 模态框过渡动画
.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
  
  .modal-content {
    transition: transform 0.3s ease;
  }
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;
  
  .modal-content {
    transform: scale(0.9);
  }
}

// 淡入淡出过渡
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
